<?php
if (eregi("template.php",$_SERVER['SCRIPT_NAME'])) {
    Header("Location: ../index.php");
    die();
}

require("errors.inc.php");

class Template {

    // parameters
	var $file = "default.tpl";
    var $page;
	var $tplDirectory = "template/lib/"; // Directory with the template files
	var $noTagsError  = false; // whether an error should be printed
	                           // if no tags are designated
	
	
	// {{{ getTpl()
    //
    // Find the template file and get its content.
    //
    // IN: string (optional) [$template: the name of the template file
	//     that should be in the directory 'template/lib']
    // OUT: -
    //
    function getTpl($template = "default.tpl")
    {
		$template = $this->tplDirectory.$template;
		
        if (file_exists($template))
		    $this->page = join("", file($template));
		else
		    die(TPL_ERROR_TPL_NOT_FOUND."<br>File name: 'template/$template'");
    }
    // }}}


	// {{{ replaceTags()
    //
    // Replace the defined tags in the template ($this->page).
    //
    // IN: array [$tags: the tag to replace, tag => content],
    // OUT: -
    //
	function replaceTags($tags = array())
	{
		if (sizeof($tags) > 0)
		    foreach ($tags as $tag => $data)
			{
			    $this->page = eregi_replace("{".$tag."}", $data, $this->page);
			}
	    elseif ($this->noTagsError == true)
		    die(TPL_ERROR_NO_TAGS_DESIGNATED);
	}
	// }}}


	// {{{ replaceCommonTags()
    //
    // Replace the tags in the template ($this->page) which are to find on every page.
    //
    // IN: -,
    // OUT: -
    //
	function replaceCommonTags()
	{
	    global $menu,$html,$common;
        $this->replaceTags(array(
		#TITLE		=>	"Universal Classes System for PHP",
		CHARSET     => _CHARSET,
		SELECTLANG  =>  $common->selectLang(),
		LOGO        =>  $common->logo()
		));
	}
	// }}}


    function parse($text = "", $check = false)
	{		
	    // php - tag
	    $text = $this->phptag(" ".$text." ");
		
	    // i,b,u,center,text - tags
	    $text = eregi_replace( "\[(b|i|u|center|span)\]", "<\\1>", $text);
	    $text = eregi_replace( "\[(b|i|u|center|span)=([a-z0-9_]+)\]", "<\\1 class='\\2'>", $text);
	    $text = eregi_replace( "\[(b|i|u|center|span)=#([a-f0-9]{6})\]", "<\\1 style='color:#\\2'>", $text);
	    $text = eregi_replace( "\[/(b|i|u|center|span)\]", "</\\1>", $text);
	    
	    //list - tag
	    $text = eregi_replace( "\[list\]", "<ul>", $text);
	    $text = eregi_replace( "\[list=(1|a|i|disc)\]", "<ol type='\\1'>", $text);
	    $text = eregi_replace( "\[\*\]", "<li>", $text);
	    $text = eregi_replace( "\[/list\]", "</ul>", $text);
		$text = eregi_replace( "\[/list=(1|a|i|disc)\]", "</ol>", $text);
	    
	    //text - tag
	    $text = eregi_replace( "\[text=([a-z0-9_]+)\]", "<span class='\\1'>", $text);
	    $text = eregi_replace( "\[text=#([a-f0-9]{6})\]", "<span style='color:#\\1'>", $text);
	    $text = eregi_replace( "\[/text\]", "</span>", $text);
	    
	    //line - tag
	    $text = eregi_replace( "\[line\]", "<hr size='1'>", $text);
	    $text = eregi_replace( "\[line=([0-9]+[%]?)\]", "<hr size='1' width='\\1'>", $text); // entweder in pixel oder in %
	    
	    //size - tag
	    $text = eregi_replace( "\[size=([1-3]?[0-9])\]", "<span style='font-size:\\1px;line-height:normal'>", $text); // max. 39 px
	    $text = eregi_replace( "\[/size\]", "</span>", $text);
	    
	    //url - tag
		    // matches a [url]xxxx://www.phpbb.com[/url] code..
		    $text = eregi_replace( "\[url\]([a-z0-9]+://[^ \"\n\r\t<]*)\[/url\]", "<a href='\\1'>\\1</a>", $text);
	    
		    // [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).
		    $text = eregi_replace( "\[url\]((www|ftp)\.[^ \"\n\r\t<]*)\[/url\]", "<a href='http://\\1'>\\1</a>", $text);
	    
		    // [url=xxxx://www.phpbb.com]phpBB[/url] code..
		    $text = eregi_replace( "\[url=([a-z0-9]+://[^ \"\n\r\t<]*)\](.*)\[/url\]", "<a href='\\1'>\\2</a>", $text);
	    
		    // [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).
		    $text = eregi_replace("\[url=((www|ftp)\.[^ \"\n\r\t<]*)\](.*)\[/url\]", "<a href='http://\\1'>\\3</a>", $text);
			
			// local link (id is given)
			$text = eregi_replace( "\[url=([0-9]+)\]", "<a href='".GLOBAL_SELF."?id=\\1'>", $text);
			$text = eregi_replace( "\[/url\]", "</a>", $text);
	    
	    // img - tag
	    $text = eregi_replace( "\[img=(.+)\]", "<img src='\\1' border='0'>", $text);
	    
	    // email - tag
	    $text = eregi_replace( "\[email\]([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4}))\[/email\]", "<a href='mailto:\\1'>\\1</a>", $text);
	    
		// h - tag
		$text = eregi_replace( "\[h\]", "<h1>", $text);
		$text = eregi_replace( "\[/h\]", "</h1>", $text);
		
	    // br - tag
	    $text = eregi_replace( "\[nl\]", "<br/>", $text);
	    
		// p - tag
        $text = eregi_replace( "\[p=(left|right|center|justify)\]", "<p style='margin-top:0px;margin-bottom:0px' align='\\1'>", $text);
        $text = eregi_replace( "\[/p\]", "</p>", $text);
		
		// anchor - tag
        $text = eregi_replace( "\[anchor=([a-z0-9_]+)\]", "<a name='\\1'></a>", $text);
		
	    #code
		
		if ($check == true) // red marked by error
		{
            $text = eregi_replace( "(\[[^]]{0,}\])", "<span style='background-color:#ff0000'>\\1</span>", $text);
		}
		
		return $text;
	}


    function phptag ($str = "")
	{
	    global $table,$db,$html,$html_form;
        if (!eregi("\[php\]", $str)) { return $str; }
        else
        {
			    $phppos   = strpos($str, "[php]")*1;
                $text     = substr($str, 0, $phppos); // Teil vor dem PHP auschneiden
                $str      = substr($str, $phppos+5, strlen($str));
                $phppos   = strpos($str, "[/php]")*1;
                $evalpart = substr($str, 0, $phppos);
	            $str      = substr($str, $phppos+6, strlen($str));
		    #echo "TEXT=$str<br>";
		    eval($evalpart);
            return $text.$this->phptag($str);
		}
    }

}
?>